<?php
namespace app\first\controller;

use app\first\model\DreamComments;
use app\first\model\Keywords;
use app\first\model\ShopGoods;
use app\first\model\ShopGoodsOrder;
use app\first\model\ShopGoodsRefund;
use app\first\model\ShopTransfers;
use service\Category;
use think\Db;
use think\db\Where;
use think\facade\Config;
use Yansongda\Pay\Pay;

/**
 * Class Shop
 * @title 商城相关
 * @url /v1/shop
 * @desc  获取梦想加相关
 * @version 1.0
 * @package app\first\dream
 * @route('v1/shop')
 */
class Shop extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = true;
    protected $model = null;
    protected $validate = null;
    protected $wechat = null;
    /*
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];

    protected $skipAuthActionList= ['index','search','last_publish','query_goods'];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Shop();
        $this->validate = new \app\first\validate\Shop();
        $this->wechat = Pay::wechat(Config::get('wechat.'));
    }

    /**
     * @title 申请开店
     * @method apply_for
     * @param string $title 商铺名 true
     * @param int $shop_type 类型 false 1 1个人,2企业
     * @param string $logo 店铺logo true logo地址
     * @param string $city_id 城市代码 true
     * @param string $location 城市经纬度 true
     * @param string $real_name 真实姓名 true
     * @param string $contact 联系方式 true
     * @param string $card_id 身份证 true
     * @param string $shop_licence 营业执照 false  多个使用','隔开
     * @route('v1/shop/apply_for','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function apply_for($title='',$shop_type=1,$logo='',$city_id='',$location='',$real_name='',$contact='',
                              $card_id='',$shop_licence='')
    {
        $result = $this->model->addNew(self::$uid,$title,$shop_type,$logo,$city_id,$location,$real_name,
            $card_id,$shop_licence,$contact,$out);
        if(!$result){
            return $this->sendError('',$out);
        }
        return $this->sendSuccess('','恭喜你申请成功，管理人员将在1-2天内完成审核');
    }

    /**
     * @title 激活店铺
     * @method active
     * @param int $id 店铺id true
     * @route('v1/shop/active','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function active($id=0){
        $result = $this->model->active($id,$out);
        if(!$result){
            return $this->sendError('',$out);
        }
        return $this->sendSuccess('','恭喜您店铺激活成功，现在可以使用店铺了');
    }

    /**
     * @title 获取上架店铺列表
     * @method index
     * @param string $city_id 城市id true
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 每页条数 true 8
     * @param int $page 当前页数 true 1
     * @route('v1/shop/list','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function index($city_id='',$is_top=0,$limit=8,$page = 1){
        $map = [['status','eq',4]];
        if($city_id){
            array_push($map,['city_id','eq',$city_id]);
        }
        if($is_top){
            array_push($map,['is_top','eq',1]);
        }
        $order= 'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取店铺详情
     * @method read
     * @param int $id id false  默认获取自己的(eg:v1/shop/read/1)
     * @route('v1/shop/read','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        $_id = $id?$id:self::$uid;
        $info = $this->model->getOneByUid($_id);
        return $this->sendSuccess($info);
    }

    /**
     * @title 获取最新上架店铺
     * @method last_publish
     * @param int $city_id 城市id true
     * @param int $limit 条数 false 10
     * @route('v1/shop/last_publish','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function last_publish($city_id=0,$limit=10){
        $list = $this->model->lastList($city_id,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 搜索店铺
     * @method search
     * @param string $q 关键词 true
     * @param int $limit 条数 true 10
     * @param int $page 页数 true 1
     * @route('v1/shop/search','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function search($q='',$limit=10,$page=1){
        if(!$q){
            return $this->sendError(0,'请输入关键词');
        }
        $where = new Where();
        $where['status']=2;
        $keywords = new Keywords();
        $keywords->addNew($q,2);
        $list = $this->model->search($q,$where,$limit);
        return $this->sendSuccess($list,'');
    }

    /**
     * @title 添加商品
     * @method create_goods
     * @param int $shop_id 店铺id true
     * @param string $title 商品名 true
     * @param string $information 商品介绍 false
     * @param string $image 商品封面图 true
     * @param string $pics 商品详情图ID true  多个使用','隔开
     * @param string $content 详情文字 false
     * @param string $price 商品售价 true  保留2位小数
     * @param string $discount 优惠金额 false
     * @param int $stocks 库存 true
     * @route('v1/shop/create_goods','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function create_goods($shop_id=0,$title='',$information='',$image='',$pics='',$content='',$price='',$discount='',$stocks=0){
        $goods = new ShopGoods();
        $result = $goods->addNew(self::$uid,$shop_id,$title,$information,$image,$pics,$content,$price,$discount,$stocks,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','添加成功');
    }

    /**
     * @title 查询商品
     * @method query_goods
     * @param int $status 状态 false 0 状态:1申请中,2审核失败,3审核通过,4上架,5下架,0全部
     * @param string $title 搜索标题 false
     * @param int $shop_id 店铺id false  填写时获取指定店铺商品
     * @param int $limit 每页条数 false 10
     * @param int $page 页数 true 1
     * @route('v1/shop/query_goods','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function query_goods($status=0,$title='',$shop_id=0,$limit=10,$page = 1){
        $goods = new ShopGoods();
        $where = new Where();
        if($status){
            $where['status'] = $status;
        }
        if($shop_id){
            $where['shop_id'] = $shop_id;
        }
        $field = 'id,title,information,image,price,discount,stocks,status,create_time';
        $list = $goods->getList($title,$where,'create_time desc',$limit,$field,false);
        return $this->sendSuccess($list);
    }
    /**
     * @title 获取所有商品
     * @method query_goods_all
     * @param int $status 状态 false 0 状态:1申请中,2审核失败,3审核通过,4上架,5下架,0全部
     * @param string $title 搜索标题 false
     * @param int $shop_id 店铺id false  填写时获取指定店铺商品
     * @route('v1/shop/query_goods_all','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function query_goods_all($status=0,$title='',$shop_id=0){
        $goods = new ShopGoods();
        $where = new Where();
        if($status){
            $where['status'] = $status;
        }
        if($shop_id){
            $where['shop_id'] = $shop_id;
        }
        if($title){
            $where['title'] = ['like',"%{$title}%"];
        }
        $list = $goods->getAllList($where,'create_time desc');
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取商品详情
     * @method goods_details
     * @param int $id 商品id true
     * @route('v1/shop/goods_details','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goods_details($id=0){
        $goods = new ShopGoods();
        $info = $goods->getOneById($id);
        return $this->sendSuccess($info);
    }

    /**
     * @title 创建订单
     * @method create_order
     * @param int $uid 购买用户id true
     * @param int $shop_id 商铺id true
     * @param array $goods 购买商品 true  商品数组对象:[{商品id:商品数量},{商品id1:商品数量1},...]
     * @param string $title 订单主题 false
     * @param string $information 订单备注 false
     * @param string $totals 总价 true
     * @param string $address 收货信息 true  收件人,收货地址
     * @param string $discount 优惠价格 false
     * @route('v1/shop/create_order','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create_order($uid=0,$shop_id=0,$goods=[],$title='',$information='',$totals='',$address='',$discount=''){
        $order = new ShopGoodsOrder();
        $result = $order->addNew($uid,$shop_id,$goods,$title,$information,$totals,$address,$discount,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess($out);
    }

    /**
     * @title 获取支付签验
     * @method signature
     * @param string $title 主题 true
     * @param string $order_id 订单号 true
     * @param string $totals 订单金额 true
     * @param int $type 支付类型 true 1 1微信,2支付宝
     * @param string $open_id openid false  微信支付必填
     * @route('v1/shop/signature','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function signature($title='',$order_id='',$totals='',$type=1,$open_id=''){
        $signature = (new Payment())->signature($type,$open_id,$order_id,$totals,$title);
        if($type==2){
         return json($signature);
        }else{
            return $this->sendSuccess($signature);
        }
    }

    /**
     * @title 我的订单
     * @method my_order
     * @param int $uid 用户id true
     * @param int $status 状态 true 0 1未支付,2支付成功,3支付失败,4出库中,5物流配送,6交易完成,7评价完成,8出库失败,9申请退款,10退款完成,0全部
     * @param int $limit 每页数量 false 10
     * @param int $page 页数 true
     * @route('v1/shop/my_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_order($uid=0,$status=0,$limit=10,$page=1){
        $uid= $uid ? $uid: self::$uid;
        $where = new Where();
        $where['uid']=$uid;
        $where['status'] = ['neq',-1];
        if($status){
            $where['status']=$status;
        }
        $order = new ShopGoodsOrder();
        $list = $order->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 店铺订单
     * @method shop_order
     * @param int $shop_id 商品id true
     * @param int $status 状态 true 0 1未支付,2支付成功,3支付失败,4出库中,5物流配送,6交易完成,7评价完成,8出库失败,9申请退款,10退款完成,0全部
     * @param int $limit 每页数量 false 10
     * @param int $page 页数 true
     * @route('v1/shop/shop_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function shop_order($shop_id=0,$status=0,$limit=10,$page=1){
        $where = new Where();
        $where['shop_id']=$shop_id;
        $where['status'] = ['neq',-1];
        if($status){
            $where['status']=$status;
        }
        $order = new ShopGoodsOrder();
        $list = $order->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 订单详情
     * @method order_detail
     * @param int $id 订单id true
     * @route('v1/shop/order_detail','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_detail($id=0){
        $order = new ShopGoodsOrder();
        $info = $order->getOneById($id);
        return $this->sendSuccess($info);
    }

    /**
     * @title 取消订单
     * @method cancel_order
     * @param int $id 订单id true
     * @route('v1/shop/cancel_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancel_order($id=0){
        $order = new ShopGoodsOrder();
        $info = $order->find($id);
        if(!$info){
            return $this->sendError(0,'订单不存在');
        }
        $refund = new ShopGoodsRefund();
        Db::startTrans();
        $result = $refund->addNew($id,$out);
        $info->status = 9;
        $result1 = $info->save();
        if($result && $result1){
            Db::commit();
            return $this->sendSuccess('','订单取消成功，客服会在1-2工作日内完成退款');
        }else{
            Db::rollback();
            return $this->sendError(0,'订单取消失败');
        }
    }

    /**
     * @title 商铺退款申请
     * @method shop_cancel_order
     * @param int $id 商铺id true
     * @param int $limit 条数 true 10
     * @param int $page 页数 true 1
     * @route('v1/shop/shop_cancel_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function shop_cancel_order($id=0,$limit=10,$page=1){
        $order = new ShopGoodsRefund();
        $where = ['shop_id','eq',$id];
        $list = $order->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 确认订单
     * @method confirm_order
     * @param int $id 订单id true
     * @route('v1/shop/confirm_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function confirm_order($id=0){
        $order = new ShopGoodsOrder();
        $info = $order->find($id);
        if(!$info){
            return $this->sendError(0,'订单不存在');
        }
        $info->status = 4;
        if(!$info->save()){
            return $this->sendError(0,'订单确认失败');
        }
        return $this->sendSuccess('','订单确认成功');
    }

    /**
     * @title 商家确认退款
     * @method confirm_refund
     * @param int $id 订单id true
     * @param int $type 类型 true 1同意,2拒绝
     * @param string $info 理由 false 拒绝必须
     * @route('v1/shop/confirm_refund','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function confirm_refund($id=0,$type=1,$info=''){
        $_info = (new ShopGoodsOrder())->find($id);
        if(!$_info){
            return $this->sendError(0,'订单不存在');
        }
        $_order = (new ShopGoodsRefund())->where('oid','eq',$id)->find();
        if($_order['status']!=1){
            return  $this->sendError('退款处理');
        }
        $_order->info = $info;
        if($type==2 && empty($info)){
            return $this->sendError('请填写退款理由');
        }
        Db::startTrans();
        $_order->status = ($type==1)?2:3;
        $result = true;
        if($type==2){
            $_info->status = 11;
            $result = $_info->save();
        }
        $result1 = $_order->save();
        if($result1 && $result){
            Db::commit();
            return $this->sendSuccess('','订单确认成功');
        }else{
            Db::rollback();
            return $this->sendError(0,'订单确认失败');
        }
    }

    /**
     * @title 完成订单
     * @method finish_order
     * @param int $id 订单id true
     * @route('v1/shop/finish_order','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function finish_order($id=0){
        $order = new ShopGoodsOrder();
        $info = $order->find($id);
        if(!$info){
            return $this->sendError(0,'订单不存在');
        }
        $info->status = 6;
        $info->end_time = time();
        if(!$info->save()){
            return $this->sendError(0,'订单完成失败');
        }
        return $this->sendSuccess('','订单完成成功');
    }

    /**
     * @title 申请提现
     * @method transfers
     * @param int $uid 用户id true
     * @param int $shop_id 商铺id true
     * @param int $amount 提现金额 true 0 0全部
     * @param string $title 主题 false 账户提现
     * @param int $type 类型 false 1 1微信,2支付宝
     * @route('v1/shop/transfers','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function transfers($uid=0,$shop_id=0,$amount=0,$title='账户提现',$type=1){
        $uid = $uid?$uid:self::$uid;
        $shop = new ShopTransfers();
        $result = $shop->addNew($uid,$title,$shop_id,$type,$amount,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','恭喜您申请成功，我们会在1-2个工作日内完成退款！');
    }

    /**
     * @title 提现记录
     * @method my_transfers
     * @param int $shop_id 商铺id true
     * @param int $limit 每页条数 false 10
     * @param int $page 页数 true 1
     * @route('v1/shop/my_transfers','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_transfers($shop_id=0,$limit=10,$page=1){
        $shop = new ShopTransfers();
        $where = new Where();
        $where['shop_id'] = $shop_id;
        $list = $shop->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }
}